#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>
#include "InetAddress.h"

InetAddress::InetAddress(uint16_t &port)
{
    // 绑定 IP
    addr_.sin_family = AF_INET;
    addr_.sin_port = ntohs(port);
    addr_.sin_addr.s_addr = INADDR_ANY;
}
InetAddress::InetAddress(const sockaddr_in addr):addr_(addr)
{
}

InetAddress::InetAddress()
{
}
InetAddress::~InetAddress()
{
}

void InetAddress::setaddr(struct sockaddr_in addr)
{
    addr_=addr;
}

// 网络传输过来
const char *InetAddress::ip() const // 返回字符串表示的地址，例如：192.168.150.128
{
    return inet_ntoa(addr_.sin_addr);
}

uint16_t InetAddress::port() const // 返回整数表示的端口，例如：80、8080
{
    return ntohs(addr_.sin_port);
}

sockaddr *InetAddress::addr() const // 返回addr_成员的地址，转换成了sockaddr。
{
    return (struct sockaddr *)&addr_;
}

sockaddr_in InetAddress::getSockAddr_in() const
{
    return addr_;
}